#ifndef OBJECTCONTROLLER_H_
#define OBJECTCONTROLLER_H_
#include "objects.h"
#include <MAUtil/Moblet.h>
#include <NativeUI/Widgets.h>
#include <NativeUI/WidgetUtil.h>
#include <MAUtil/Graphics.h>
#include "MAHeaders.h"
#include "player.h"
#include <conprint.h>

using namespace MAUtil;
using namespace NativeUI;

class ObjectController
{
public:
	ObjectController(Camera* camera)
	{


		MAExtent screenSize = maGetScrSize();
	    mScreenWidth = EXTENT_Y(screenSize);
		mScreenHeight = EXTENT_X(screenSize);
		cam = camera;

		//p_vector = Vector<Platform>();


		//o_vector = Vector<Orb>();

		o_min = o_max = p_min = p_max = 0;

		Generate();
	}

	void Generate()
	{
		srand(maTime());
		int maxSlots = 200;
		int slot = 8;
		int gridy = 0;
		int mod = 0;
		bool gen = false;
		bool block = false;
		bool force_block = false;
		bool block_prev = false;
		bool orb = false;
		for(slot; slot<maxSlots; slot++)
		{

			if(block == true)
				block_prev = true;

			gen = false;
			block = false;
			orb = false;

			//do we generate
			if(rand()%100 <80)
			{
				gen = true;
			}

			//must generate a block if the previous block is floating
			if(block_prev == true && gridy >0)
			{
				force_block = true;
			}

			//do we generate a block
			if((rand()%100 <50 && gen == true) || force_block == true)
			{
				block = true;
				//does the block go up or down
				if(rand()%100 < 80)
				{
				if(block_prev == true && rand()%100<50 && gridy <6)
				{
					gridy++;
				}
				else if(block_prev == true && gridy > 0)
				{
					gridy--;
				}
				}
			}
			//do we generate an orb
			if(rand()%100 <75 && gen == true)
			{
				orb = true;
			}
			//is the orb on top of a block
			if(orb == true && block == true)
			{
				if(rand()%100 <25)
				{
					o_vector.add(Orb(50*slot, mScreenHeight -100 - ((gridy+1)*50), 0));
				}
				else
				{
					o_vector.add(Orb(50*slot, mScreenHeight -100 - ((gridy+1)*50), 1));
				}
			}//is the orb on the ground
			else if (orb == true)
			{
				if(rand()%100 <25)
				{
					o_vector.add(Orb(50*slot, mScreenHeight -100 - ((gridy)*50), 0));
				}
				else
				{
					o_vector.add(Orb(50*slot, mScreenHeight -100 - ((gridy)*50), 1));
				}
			}

			if(block == true)
			{
				p_vector.add(Platform(50*slot,mScreenHeight -100-(gridy*50),rand()%4));
			}

			block_prev = false;
			force_block = false;
		}
		p_max = p_vector.size();
		o_max = o_vector.size();
	}

	void draw()
	{
		int x, y, c;

		for(int i = 0; i<=p_vector.size(); i++)
		{
			x = p_vector[i].getx();
			y = p_vector[i].gety();
			c = p_vector[i].getc();
			x = x - cam->xOffset();
			y = y - cam->yOffset();
			p_vector[i].draw(x, y, c);

		}

		for(int i = 0; i<=o_vector.size(); i++)
		{
			x = o_vector[i].getx();
			y = o_vector[i].gety();
			x = x - cam->xOffset();
			y = y - cam->yOffset();
			o_vector[i].draw(x,y);
		}

		/*for(int i = p_min; i<=p_max; i++)
				{
					x = p_vector[i].getx();
					y = p_vector[i].gety();
					x = x - cam->xOffset();
					y = y - cam->yOffset();
					p_vector[i].draw(x, y);
					if(x < -50)
					{
						p_min++;
					}
					if(x> mScreenWidth)
					{
						p_max = i+1;
					}
				}

				for(int i = o_min; i<=o_max; i++)
				{
					x = o_vector[i].getx();
					y = o_vector[i].gety();
					x = x - cam->xOffset();
					y = y - cam->yOffset();
					o_vector[i].draw(x, y);
					if(x < -50)
					{
						o_min++;
					}
					if(x> mScreenWidth)
					{
						o_max = i+1;
					}
				}*/
	}

	void p_collide(Player* pl)
	{
		int x, y = 0;
		int bottom = 46;
		int right = 45;
		int left = 10;

		for(int i = 0; i<=p_vector.size(); i++)
		{
			x = p_vector[i].getx();
			y = p_vector[i].gety();

			int plx = pl->GetLoc_x() + right;
			int ply = pl->GetLoc_y() + bottom;

			//char str[255];
			//sprintf(str, "%s", "collision");
			//Gfx_drawText(5,50, str);
			if(plx > x && ply > y && plx < x+50)
			{
				//char str[255];
				//sprintf(str, "%s", "collision");
				plx = plx - x;
				plx = pl->GetLoc_x() - plx;
				pl->SetLoc_x(plx);
			}

			if(plx > x && ply >y && pl->GetLoc_y() < y && pl->GetLoc_x() < x+50)
			{
				ply = ply -y;
				ply = pl->GetLoc_y() - ply;
				pl->SetLoc_y(ply);
				pl->SetSpd_y(0);
			}
			/*ply = pl->GetLoc_y() + bottom;
			if(ply > mScreenHeight - 100)
			{

				pl->SetLoc_y(mScreenHeight -101);
			}*/

		}

	}

	bool o_collide(Player* pl)
	{
		int x, y = 0;

		int bottom = 46;
		int right = 45;
		int top = 16;
		int left = 17;
		for(int i = 0; i<=o_vector.size(); i++)
		{
			x = o_vector[i].getx();
			y = o_vector[i].gety();

			if(pl->GetLoc_x() +right > x+left && pl->GetLoc_y()+bottom > y+top
					&& pl->GetLoc_x() < x +31 && o_vector[i].isDrawn())
			{
				pl->orbcollide(o_vector[i].getColor());
				o_vector[i].deactivate();
				return true;
			}
		}
		return false;
	}

private:

	int o_min, o_max;
	int p_min, p_max;
	int mScreenWidth;
	int mScreenHeight;
	Camera* cam;
	Vector<Platform> p_vector;
	Vector<Orb> o_vector;
};





#endif
